<div class="content container">

    <section class="make row">
        <a onclick="saveexam()" class="btn btn-primary pull-right">生成试卷</a>
        <div class="form-group clearfix">
            <label for="newPwd" class="col-sm-1">名目:</label>
            <div class="col-sm-10">
                <input id="title" type="text" class="form-control">
            </div>
            <label for="newPwd" class="col-sm-1">本卷类型:</label>
            <div class="col-sm-10">
                <select class="form-control" id="select0">
                    <option value="0">考试</option>
                    <option value="1">作业</option>
                </select>
            </div>
            <label for="newPwd" class="col-sm-1 time1">考试时长:</label>
            <div class="col-sm-10 time1">
                <input id="totaltime" value="0" type="number" class="form-control" placeholder="请输入时长（分钟）">
            </div>
            <label for="newPwd" class="col-sm-1 time2 hide">截止时间:</label>
            <div class="col-sm-10 time2 hide">
                <input id="tasktime" value="0" type="text" class="form-control form-datetime" placeholder="单击选择时间">
            </div>
        </div>
        <div class="form-group clearfix pt-40">
            <label for="newPwd" class="col-sm-1">单选题:</label>
            <div class="col-sm-1">
                <input  placeholder="分/题" id="dxc" onblur="allcount()" value="0" type="text" class="form-control"> 
            </div>
            <label class="col-sm-1">分/题</label>
            <label for="newPwd" class="col-sm-1">多选题:</label>
            <div class="col-sm-1">
                <input id="dxzc" onblur="allcount()" value="0" type="text" class="form-control"> 
            </div>
            <label class="col-sm-1">分/题</label>
            <label for="newPwd" class="col-sm-1">判断题:</label>
            <div class="col-sm-1">
                <input id="pdc" onblur="allcount()" value="0" type="text" class="form-control"> 
            </div>
            <label class="col-sm-1">分/题</label>
        </div>
        <div class="form-group clearfix">
            <label for="newPwd" class="col-sm-1">填空题:</label>
            <div class="col-sm-1">
                <input id="tkc" onblur="allcount()" value="0" type="text" class="form-control">
            </div>
            <label class="col-sm-1">分/题</label>
            <label for="newPwd" class="col-sm-1">主观题:</label>
            <div class="col-sm-1">
                <input id="zgc" onblur="allcount()" value="0" type="text" class="form-control">
            </div>
            <label class="col-sm-1">分/题</label>
        </div>
    </section>

    <!-- container -->
    <section class="subject">
        <div class="form-group clearfix row pt-40">
            <div class="col-sm-2">
                <select class="form-control" id="select1">
                    <option value="-1">请选择题库</option>
                    <option value="0">公共题库</option>
                    <option value="1">自建题库</option>
                </select>
            </div>
            <div class="col-sm-2">
                <select class="form-control" style="display:none;" id="select2">
                </select>
            </div>
            <div class="col-sm-2">
                <select class="form-control" style="display:none;" id="select3">
                </select>
            </div>
            <div class="col-sm-2">
                <select class="form-control" style="display:none;" id="select4">
                </select>
            </div>
        </div>
        <p class="count">
            共<span class="count0">0</span>题： 
            单选题<span class="count1">0</span> 
            多选题<span class="count2">0</span> 
            判断题<span class="count3">0</span> 
            填空题<span class="count4">0</span> 
            主观题<span class="count5">0</span> 　　
            总分：<span class="count6">0</span>
        </p>

        <div class="box row">
            <div class="col-lg-6">
                <h2>已选题目</h2>
                <section class="exam-list">
                    <h3>单选题</h3>
                    <div id="dx"></div>

                    <h3>多选题</h3>
                    <div id="dxz"></div>

                    <h3>判断题</h3>
                    <div id="pd"></div>

                    <h3>填空题</h3>
                    <div id="tk"></div>

                    <h3>主观题</h3>
                    <div id="zg"></div>
                </section>
            </div>

            <!-- 选择题目 -->
            <div class="col-lg-6">
                <h2>选择题目</h2>
                <section class="subject-select">
                    <div class="make-search clearfix">
                        <input id="selque" type="text">
                        <a onclick="clselque()" class="btn btn-success">重置</a>
                        <a onclick="onselque()" class="btn btn-primary">搜索</a>
                    </div>
                    <div class="col-sm-12">
                        
                      

                    </div>
                </section>
            </div>
            <!-- 选择题目 End -->
            
        </div>
    </section>
    <!-- container End -->

</div>
<script src="__PUBLIC__/dist/js/jquery.js"></script>
<script src="__PUBLIC__/dist/js/datetimepicker.min.js"></script>
<script>
   // $(function(){
        // 日期选择器
        // API地址 http://www.bootcdn.cn/bootstrap-datepicker/readme/
        $(".form-datetime").datetimepicker({
            todayBtn: 1,
            autoclose: 1,
            todayHighlight: 1,
            startView: 2,
            showMeridian: 1,
            format: "yyyy-mm-dd hh:ii",
            startDate: new Date()
        });
        $(".make").on("change","select",function(){
                var val = Number($(this).val());
                console.log(val)
                if(val){
                    $(".time1").hide();
                    $(".time2").show();
                }else{
                    $(".time1").show();
                    $(".time2").hide();
                }
            })
            var json={$zyjson};
            $("#select1").on("change",function(){
                var val = Number($(this).val());
                //console.log(val)
                if(val == 0){
                    // 自建题库
                    $("#select2").show();
                    $("#select4").hide();

                    var html = '<option value="0">选择专业</option>';
                    for(var x in json){
                        html+= '<option value="' + json[x].id + '">' + json[x].title + '</option>';
                    }
                    $("#select2").html(html);

                
                    $("#select2").on("change",function(){
                        // 选择专业
                        $("#select3").show();

                        var i = Number($(this).val());
                        var json2=[];
                        for(var a in json)
                        {
                            if(json[a].id==i)
                            {
                                json2=json[a].gwlist;
                            }
                        }
                        //var json2 = json[i].gwlist;

                        var html2 = '<option value="0">选择岗位</option>';
                        for(var x in json2){
                            html2+= '<option value="' + json2[x].id + '">' + json2[x].title + '</option>';
                        }
                        $("#select3").html(html2);

                        $("#select3").on("change",function(){
                            // 选择专业
                            $("#select4").show();
                            var json3 = [];
                            var i = Number($(this).val()) ;
                            for(var a in json2)
                            {
                                if(json2[a].id==i)
                                {
                                    json3=json2[a].xmlist;
                                }
                            }
                             

                            var html3 = '<option value="0">选择岗位</option>';
                            for(var x in json3){
                                html3+= '<option value="' + json3[x].id + '">' + json3[x].title + '</option>';
                            }
                            $("#select4").html(html3);

                            
                        })
                    })

                }else if(val == 1){
                    // 公共题库
                    var questiontype=$("#select1").val();
                    getlist(questiontype,0,'');
                    $("#select2").hide();
                    $("#select3").hide();
                    $("#select4").hide();
                }
            })
            $("#select4").on("change",function(){
                var val = Number($(this).val());
                if(val == 0 ) return;
                var questiontype=$("#select1").val();
                getlist(questiontype,val,'');
                //var json=[{"id":"1","content":"\u7f8e\u56fd\u7684\u72ec\u7acb\u6218\u4e89\u662f\u54ea\u4e00\u5e74","type":"1","answeroptions":"A\uff1a1820\r\nB\uff1a1821\r\nC\uff1a1822\r\nD\uff1a1823","answer":"A"},{"id":"2","content":"\u4e2d\u56fd\u52a0\u5165wto\u662f\u54ea\u4e00\u5e74\uff1f","type":"1","answeroptions":"A:1952\r\nB\uff1a1953\r\nC\uff1a1958\r\nD\uff1a1960","answer":"D"},{"id":"4","content":"adasdas","type":"1","answeroptions":"sdfsdf","answer":"a"}];
                //aJson(json);

            });
            //选择题目
            var alljson=[];
            //已选题目
            var yxjson=[];
            
            //get选择题目列表
            function getlist(questiontype,xmid,content)
            {
                var data = '';
                var url =  "{:U('User/Teacher/questionlist')}&questiontype="+questiontype+"&xmid="+xmid+"&content="+content;
                $.ajax({
                    type: "POST",
                    url: url,
                    data: data,
                    success: function(msg){
                         
                        aJson(msg);
                    }
                });
            }
            //选择题目的列表
            function aJson(json){
                json=JSON.parse(json);
                  alljson=json;
                  $(".subject-select div.col-sm-12").html('');
                if(json)
                {
                    for(var x in json){
                        var option=json[x].answeroptions.split("|");
                        var html = '<div class="exam-list-li">';
                                    html += '<input type="checkbox" value="'+x+'" id="'+json[x].id+'" data-add>';
                                    // html += '<span>'+ json[x].id +'、</span>';

                                html += '<div class="exam-list-li-text">'+json[x].content+'</div>';
                                
                                
                                if(json[x].type != 5){
                                    html += '<div class="exam-list-li-answer">';
                                    for(var c in option){
                                        html += '<div class="radio">'+ option[c] +'</div>';
                                    }
                                    html += '</div>';
                                                
                                    html += '<div class="exam-answer">';
                                        html += '<div class="right">正确答案：<span>'+json[x].answer+'</span></div>';
                                    html += '</div>';
                                }
                            html += '</div>';
                        
                        $(".subject-select div.col-sm-12").append(html);
                     }
                }
            }

            $(".subject-select").on("click","[data-add]",function(){
                var c = $(this).is(':checked');
                 
                    aexlist($(this).attr("id"),c)
                    //console.log($(this).attr("id"));
                 
            })
            //已选列表的删除和添加
            //t0 add t1 del
            function aexlist(val,t)
            {
                if(alljson)
                {
                    for(var a in alljson)
                    {
                         
                        if(alljson[a].id==val)
                        {
                        //     console.log(val);
                        // console.log(alljson[a].id);
                            if(t)
                            {
                                yxjson.push(alljson[a]);
                            }
                            else
                            {
                                for(var b in yxjson)
                                {
                                    if(yxjson[b].id==val)
                                    {
                                        yxjson.splice($.inArray(alljson[a], yxjson), 1);
                                    }
                                }
                            }
                        }
                    }
                }
                refxlist();
            }
            //已选列表中的删除按钮
            function delque(val)
            {
                aexlist(val,false);
            }
            //刷新已选列表
            function refxlist()
            {
 
                allcount();
                if(yxjson)
                {
                    
                    $("#dx").html('');
                    $("#pd").html('');
                    $("#tk").html('');
                    $("#zg").html('');
                    $("#dxz").html('');
                    var dx='',pd='',tk='',zg='',dxz='';
                    //var i=1;
                    yxjson.forEach(element => {
                        var option=element.answeroptions.split("|");
                        switch (element.type) {
                            case '1':
                                dx+=`
                                <div class="exam-list-li">
                                    <a class="close" onclick="delque(`+element.id+`)">×</a>
                                    <div class="exam-list-li-text">
                                        `+element.content+`
                                    </div>`
                                 +` <div class="exam-list-li-answer">`
                                        dx += `<div class="exam-list-li-answer">`;
                                        for(var c in option){
                                            dx += `<div class="radio">`+option[c]+`</div>`;
                                        }
                                        dx += `</div>`;
                                dx += `</div></div>`;
                                break;
                            case '2':
                                dxz+=`
                                <div class="exam-list-li">
                                    <a class="close" onclick="delque(`+element.id+`)">×</a>
                                    <div class="exam-list-li-text">
                                        `+element.content+`
                                    </div>`

                                 +` <div class="exam-list-li-answer">`
                                    dxz += `<div class="exam-list-li-answer">`;
                                    for(var c in option){
                                        dxz += `<div class="radio">`+option[c]+`</div>`;
                                    }
                                    dxz += `</div>`;
                                dxz += `</div></div>`;
                                break;
                            case '3':
                                pd+=`
                                <div class="exam-list-li">
                                    <a class="close" onclick="delque(`+element.id+`)">×</a>
                                    <div class="exam-list-li-text">
                                        `+element.content+`
                                    </div>`

                               +` <div class="exam-list-li-answer">`
                                    pd += `<div class="exam-list-li-answer">`;
                                    for(var c in option){
                                            pd += `<div class="radio">`+option[c]+`</div>`;
                                    }
                                    pd += `</div>`;
                                pd += `</div></div>`;
                                break;
                            case '4':
                                 tk+=`
                                <div class="exam-list-li">
                                    <a class="close" onclick="delque(`+element.id+`)">×</a>
                                    <div class="exam-list-li-text">
                                        `+element.content+`
                                    </div></div>`;
                                break;
                            case '5':
                                zg+=`
                                <div class="exam-list-li">
                                    <a class="close" onclick="delque(`+element.id+`)">×</a>
                                    <div class="exam-list-li-text">
                                        `+element.content+`
                                    </div></div>`;
                                break;
                        }
                    //i++;
                    });
                    //console.log(dx);
                    $("#dx").append(dx);
                    $("#pd").append(pd);
                    $("#tk").append(tk);
                    $("#zg").append(zg);
                    $("#dxz").append(dxz);
                }
                console.log(yxjson);
            }
            //计算统计题目和分数
            function allcount()
            {
                var num=0,dx=0,dxz=0,pd=0,tk=0,zg=0,total=0;
                var dxc=$('#dxc').val();
                var pdc=$('#pdc').val();
                var tkc=$('#tkc').val();
                var zgc=$('#zgc').val();
                var dxzc=$('#dxzc').val();

                if(yxjson)
                {
                    num=yxjson.length;
                    yxjson.forEach(element => {
                        switch (element.type) {
                            case '1':
                                dx++;
                                total+=Number(dxc);
                                break;
                            case '2':
                                dxz++;
                                total+=Number(dxzc);
                                break;
                            case '3':
                                pd++;
                                total+=Number(pdc);
                                break;
                            case '4':
                                tk++;
                                total+=Number(tkc);
                                break;
                            case '5':
                                zg++;
                                total+=Number(zgc);
                                break;
                        }
                    });
                }
                $('.count0').html(num)
                $('.count1').html(dx)
                $('.count2').html(dxz)
                $('.count3').html(pd)
                $('.count4').html(tk)
                $('.count5').html(zg)
                $('.count6').html(total)
                return total;
            }
            //保存
            function saveexam()
            {
                var title=$('#title').val();
                var dx=$('#dxc').val();
                var pd=$('#pdc').val();
                var tk=$('#tkc').val();
                var zg=$('#zgc').val();
                var dxz=$('#dxzc').val();
                var totaltime=$('#totaltime').val();
                var tasktime=$('#tasktime').val();
                var type=$('#select0').val();
                var pid=[];
                var total=allcount();
                if(yxjson)
                {
                    yxjson.forEach(element => {
                        pid.push(element.id);
                    });
                }
                if(title== '')
                {
                    alert('填写标题');
                    return false;
                }
                if(pid.length<=0)
                {
                    alert('选择题目');
                    return false;
                }
                if(type==0)
                {
                    if(totaltime<=0)
                    {
                        alert('考试时间不小于0');
                        return false;
                    }
                }
                else
                {
                    if(tasktime<=0)
                    {
                        alert('时间不小于0');
                        return false;
                    }
                }
                var data = {title:title,dx:dx,pd:pd,tk:tk,zg:zg,dxz:dxz,totaltime:totaltime,tasktime:tasktime,pid:JSON.stringify(pid),type:type,total:total};
                var url =  "{:U('User/Teacher/buildexam')}";
                $.ajax({
                    type: "POST",
                    url: url,
                    data: data,
                    success: function(msg){
                         msg=JSON.parse(msg);
                        if(msg.status==1)
                        {
                            if(type==0)
                            {
                                window.location.href= "{:U('User/Teacher/ExamCollect')}";
                            }
                            else
                            {
                                window.location.href= "{:U('User/Teacher/TaskCollect')}";
                            }
                        }
                         
                    }
                });
            }
            //搜索部分s
            function onselque()
            {
                var val=$('#selque').val();
                if(val)
                {
                    var id=0;
                    var questiontype=$("#select1").val();
                    if(questiontype==0)
                    {
                         id=$("#select4").val();
                    }
                    
                    getlist(questiontype,id,val);
                
                }
            }
            function clselque()
            {
                $('#selque').val('');
            }
            //搜索部分end
 
     //   })
      
</script>